package com.dippy.system.controller;

import com.dippy.system.entity.Test;
import com.dippy.system.res.Response;
import com.dippy.system.service.TestService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Test)控制层
 *
 * @author
 * @since 2020-11-13 17:13:53
 */
@RestController
@RequestMapping("/test")
public class TestController {
    /**
     * 服务对象
     */
    @Resource
    private TestService testService;

    /**
     * 通过主键查询单条数据
     *
     * @param test 参数对象
     * @return 单条数据
     */
    @RequestMapping(value = "get", method = RequestMethod.GET)
    public Response<Test> selectOne(Test test) {
        Test result = testService.selectById(test.getId());
        if (result != null) {
            Response.createSuccessResponse("查询成功", result);
        }
        return Response.createErrorResponse("查询失败");
    }

    /**
     * 新增一条数据
     *
     * @param test 实体类
     * @return Response对象
     */
    @RequestMapping(value = "insert", method = RequestMethod.POST)
    public Response<Test> insert(@RequestBody Test test) {
        int result = testService.insert(test);
        if (result > 0) {
            Response.createSuccessResponse("新增成功", test);
        }
        return Response.createErrorResponse("新增失败");
    }

    /**
     * 修改一条数据
     *
     * @param test 实体类
     * @return Response对象
     */
    @RequestMapping(value = "update", method = RequestMethod.PUT)
    public Response<Test> update(@RequestBody Test test) {
        Test result = testService.update(test);
        if (result != null) {
            Response.createSuccessResponse("修改成功", result);
        }
        return Response.createErrorResponse("修改失败");
    }

    /**
     * 删除一条数据
     *
     * @param test 参数对象
     * @return Response对象
     */
    @RequestMapping(value = "delete", method = RequestMethod.DELETE)
    public Response<Test> delete(Test test) {
        int result = testService.deleteById(test.getId());
        if (result > 0) {
            Response.createSuccessResponse("删除成功", null);
        }
        return Response.createErrorResponse("删除失败");
    }

    /**
     * 查询全部
     *
     * @return Response对象
     */
    @RequestMapping(value = "selectAll", method = RequestMethod.GET)
    public Response<List<Test>> selectAll() {
        List<Test> tests = testService.selectAll();
        if (tests != null) {
            Response.createSuccessResponse("查询成功", tests);
        }
        return Response.createErrorResponse("查询失败");
    }

    /**
     * 分页查询
     *
     * @param start 偏移
     * @param limit 条数
     * @return Response对象
     */
    @RequestMapping(value = "selectPage", method = RequestMethod.GET)
    public Response<Test> selectPage(Integer start, Integer limit) {
        List<Test> tests = testService.selectPage(start, limit);
        if (tests != null) {
            Response.createSuccessResponse("查询成功", tests);
        }
        return Response.createErrorResponse("查询失败");
    }

}
